package com.ruoyi.user.utils;

import com.aliyun.oss.OSSClient;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * @author : LiHan
 * @date : 2024-08-20 23:13
 * @description :
 **/

public class OssUtil {
    private static final String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    private static final String accessKeyId = "LTAI5tFwQvzxu2uCqZwZcV1m";
    private static final String accessKeySecret = "sSuhhaWIazkUi3DFNLbokZht4SLj4n";

    public static String Oss(MultipartFile file) throws IOException {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        String substring = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = System.currentTimeMillis() + substring;
        ossClient.putObject("quanbo-bucket", fileName, file.getInputStream());
        fileName = "https://quanbo-bucket.oss-cn-beijing.aliyuncs.com/" + fileName;
        return fileName;
    }
}
